package com.xuzz.study.spring.schema;

import com.xuzz.study.spring.simpleioc.utils.ClassUtils;
import org.springframework.beans.factory.FactoryBean;

public class ReferenceBean<T> implements FactoryBean<T> {
    /**
     * The interface name of the reference service
     */
    protected String interfaceName;

    /**
     * The interface class of the reference service
     */
    protected Class<?> interfaceClass;

    @Override
    public T getObject() throws Exception {
        //dubbo也是类似的道理，在这里返回代理对象。dubbo默认使用javaassit。也可配置使用jdk。
        //dubbo不支持cglib
        System.out.println("访问 ReferenceBean中的getObject，可以在这里通过代理生成具体的T的实例");
        return null;
    }

    @Override
    public Class<?> getObjectType() {
        return getInterfaceClass();
    }

    public Class<?> getInterfaceClass() {
        if (interfaceClass != null) {
            return interfaceClass;
        }
        try {
            if (interfaceName != null && interfaceName.length() > 0) {
                interfaceClass = Class.forName(interfaceName, true, ClassUtils.getClassLoader());
            }
        } catch (ClassNotFoundException t) {
            throw new IllegalStateException(t.getMessage(), t);
        }

        return interfaceClass;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public void setInterfaceClass(Class<?> interfaceClass) {
        this.interfaceClass = interfaceClass;
    }
}
